package demoMod.gdxform.demo.core;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import demoMod.gdxform.abstracts.Container;
import demoMod.gdxform.enums.GFrameCloseStrategy;
import demoMod.gdxform.enums.GFrameWindowMode;
import demoMod.gdxform.enums.GFrameWindowStyle;
import demoMod.gdxform.demo.fonts.FontKeys;
import demoMod.gdxform.helpers.FontHelper;
import demoMod.gdxform.interfaces.Element;
import demoMod.gdxform.ui.*;
import demoMod.gdxform.core.FormManager;

import java.io.File;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class MainWindow implements ApplicationListener {
    private SpriteBatch sb;
    private FormManager formManager;

    private void initializeFont(String fontKey, String fontPath, Consumer<FreeTypeFontGenerator.FreeTypeFontParameter> fontStyle) {
        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal(fontPath));
        FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
        fontStyle.accept(parameter);
        BitmapFont font = generator.generateFont(parameter);
        FontHelper.registerFont(fontKey, font);
    }

    @Override
    public void create() {
        sb = new SpriteBatch();

        String name = ManagementFactory.getRuntimeMXBean().getName();
        String pid = name.split("@")[0];
        System.out.println("pid is: " + pid);

        initializeFont(FontKeys.SIM_HEI_12, "fonts/simhei.ttf", parameter -> {
            parameter.incremental = true;
            parameter.characters = "";
            parameter.size = 12;
        });
        initializeFont(FontKeys.SIM_HEI_14, "fonts/simhei.ttf", parameter -> {
            parameter.incremental = true;
            parameter.characters = "";
            parameter.size = 14;
        });
        initializeFont(FontKeys.SIM_HEI_16, "fonts/simhei.ttf", parameter -> {
            parameter.incremental = true;
            parameter.characters = "";
            parameter.size = 16;
        });
        initializeFont(FontKeys.SIM_HEI_18, "fonts/simhei.ttf", parameter -> {
            parameter.incremental = true;
            parameter.characters = "";
            parameter.size = 18;
        });
        initializeFont(FontKeys.SIM_HEI_20, "fonts/simhei.ttf", parameter -> {
            parameter.incremental = true;
            parameter.characters = "";
            parameter.size = 20;
        });

        formManager = FormManager.getInstance();

        GFrame gFrameA = new GFrame(120, 150, 400, 400, FontHelper.getFont(FontKeys.SIM_HEI_14));
        gFrameA.setTitle("窗体A");
        gFrameA.setResizable(true);
        gFrameA.setId("Form1");
        gFrameA.setCloseStrategy(GFrameCloseStrategy.HIDDEN_ON_CLOSE);

        GCheckBox checkBox = new GCheckBox(192, 192, 16, 16, FontHelper.getFont(FontKeys.SIM_HEI_16)) {
            @Override
            public void onClick() {
                System.out.println("Current state:" + this.getState());
            }

            @Override
            public void resize(float deltaX, float deltaY, boolean fromParent) {
                if (fromParent) {
                    setX((getParent().getWidth() - getWidth()) / 2.0F);
                    setY((getParent().getHeight() - getHeight()) / 2.0F);
                }
            }
        };
        checkBox.setText("复选框");
        checkBox.setId("CheckBox1");
        gFrameA.addElement(checkBox);

        GButton disableChkButton = new GButton(140, 90, 120, 20, FontHelper.getFont(FontKeys.SIM_HEI_14)) {
            @Override
            public void onClick() {
                Container<? extends Element> container = FormManager.getInstance().getContainerById("Form1");
                if (container != null) {
                    Element element = container.getElementById("CheckBox1");
                    if (element != null) {
                        element.setEnabled(!element.enabled());
                    }
                }
            }

            @Override
            public void resize(float deltaX, float deltaY, boolean fromParent) {
                if (fromParent) {
                    setX((getParent().getWidth() - getWidth()) / 2.0F);
                    setY((getParent().getHeight() - getHeight()) / 2.0F - 100.0F);
                }
            }
        };
        disableChkButton.setText("启用/停用复选框");
        disableChkButton.setBackground(Color.WHITE);
        disableChkButton.setColor(Color.BLACK);
        disableChkButton.setBorderWidth(3);
        disableChkButton.setBorder(Color.BLACK);
        gFrameA.addElement(disableChkButton);

        List<GRadioButton> radioButtonGroup = new ArrayList<>();

        GRadioButton radioButton1 = new GRadioButton(50, 90, 12, 12, FontHelper.getFont(FontKeys.SIM_HEI_14));
        radioButton1.setText("单选框1");
        radioButton1.setGroup(radioButtonGroup);
        radioButton1.setId("Radio1");

        GRadioButton radioButton2 = new GRadioButton(50, 70, 12, 12, FontHelper.getFont(FontKeys.SIM_HEI_14));
        radioButton2.setText("单选框2");
        radioButton2.setGroup(radioButtonGroup);
        radioButton2.setId("Radio2");

        gFrameA.addElement(radioButton1);
        gFrameA.addElement(radioButton2);

        GFrame gFrameB = new GFrame(280, 300, 400, 400, FontHelper.getFont(FontKeys.SIM_HEI_14));
        gFrameB.setTitle("窗体B");
        gFrameB.setResizable(true);
        GButton button = new GButton(160, 190, 80, 20, FontHelper.getFont(FontKeys.SIM_HEI_14)) {
            @Override
            public void onClick() {
                Container<? extends Element> container = FormManager.getInstance().getContainerById("Form1");
                if (container != null) {
                    container.setVisible(!container.visible());
                }
            }

            @Override
            public void resize(float deltaX, float deltaY, boolean fromParent) {
                if (fromParent) {
                    setX((getParent().getWidth() - getWidth()) / 2.0F);
                    setY((getParent().getHeight() - getHeight()) / 2.0F);
                }
            }
        };
        button.setId("Button1");
        button.setText("按钮");
        button.setBackground(Color.WHITE);
        button.setColor(Color.BLACK);
        button.setBorderWidth(3);
        button.setBorder(Color.BLACK);
        gFrameB.addElement(button);
        gFrameB.setId("Form2");

        GFrame gFrameC = new GFrame(600, 300, 400, 400, FontHelper.getFont(FontKeys.SIM_HEI_14), GFrameWindowStyle.CLOSE_BUTTON_ONLY, false);
        gFrameC.setResizable(true);
        gFrameC.setId("Form3");
        gFrameC.setTitle("窗体C");
        gFrameC.setBackground(Color.CHARTREUSE);

        GFrame gFrameD = new GFrame(150, 150, 100, 100, FontHelper.getFont(FontKeys.SIM_HEI_12), GFrameWindowStyle.CLOSE_BUTTON_ONLY, true);
        gFrameD.setResizable(true);
        gFrameD.setTitle("窗体D");
        gFrameD.setId("Form4");
        gFrameD.setBackground(Color.DARK_GRAY);

        GLabel label = new GLabel(0, 0, 150, 40, FontHelper.getFont(FontKeys.SIM_HEI_14));
        label.setColor(Color.WHITE);
        label.setText("溢出测试溢出测试溢出测试溢出测试溢出测试溢出测试溢出测试溢出测试");
        label.setId("Label1");
        gFrameD.addElement(label);

        gFrameC.addElement(gFrameD);

        GTextField textField = new GTextField(20, 300, 120, 40, FontHelper.getFont(FontKeys.SIM_HEI_14));
        textField.setId("Text1");
        textField.setText("示例文本");
        textField.setPlaceholder("在此处输入文本……");

        gFrameC.addElement(textField);

        GScrollPane scrollPane = new GScrollPane(600, 30, 500, 500, 1000, 1000) {
            @Override
            public boolean hasFocus() {
                return false;
            }
        };
        scrollPane.setBackground(Color.BROWN.cpy());
        scrollPane.setId("ScrollPane1");
        formManager.addContainer(scrollPane);

        GHScrollBar hScrollBar = new GHScrollBar(0, 0, 500, 5);
        scrollPane.setHScrollBar(hScrollBar);

        GVScrollBar vScrollBar = new GVScrollBar(0, 0, 5, 500);
        scrollPane.setVScrollBar(vScrollBar);

        GLabel label1 = new GLabel(210, 740, 80, 20, FontHelper.getFont(FontKeys.SIM_HEI_16));
        label1.setColor(Color.WHITE.cpy());
        label1.setText("滚动条测试");
        scrollPane.addElement(label1);

        GList<GLabel> list = new GList<>();
        list.setCellHeight(120);
        //最多只能选两项
        list.setMaxSelectedItems(2);
        GLabel item1 = new GLabel(40, 48, 120, 30, FontHelper.getFont(FontKeys.SIM_HEI_16));
        item1.setText("item1");
        list.addItem(item1);
        GLabel item2 = new GLabel(40, 48, 120, 30, FontHelper.getFont(FontKeys.SIM_HEI_16));
        item2.setText("item2");
        list.addItem(item2);
        GLabel item3 = new GLabel(40, 48, 120, 30, FontHelper.getFont(FontKeys.SIM_HEI_16));
        item3.setText("item3");
        list.addItem(item3);
        scrollPane.addElement(list);

        GMenuList menuList = new GMenuList();
        menuList.setWidth(100.0F);
        menuList.setX(50.0F);
        menuList.setY(50.0F);

        GMenuItem menuItem1 = new GMenuItem(menuList.getWidth(), menuList.getMenuItemHeight(), "item1", FontHelper.getFont(FontKeys.SIM_HEI_14));
        GMenuItem menuItem2 = new GMenuItem(menuList.getWidth(), menuList.getMenuItemHeight(), "item2(2)", FontHelper.getFont(FontKeys.SIM_HEI_14)) {
            @Override
            public boolean isHotKeyPressed() {
                return Gdx.input.isKeyPressed(Input.Keys.NUM_2);
            }
        };

        menuItem2.setSubMenu(() -> {
            GMenuList subMenu = new GMenuList();
            subMenu.setWidth(120.0F);

            GMenuItem subMenuItem = new GMenuItem(subMenu.getWidth(), subMenu.getMenuItemHeight(), "subItem1(3)", FontHelper.getFont(FontKeys.SIM_HEI_14)) {
                @Override
                public boolean isHotKeyPressed() {
                    return Gdx.input.isKeyPressed(Input.Keys.NUM_3);
                }
            };
            subMenuItem.setSplitLine(false);

            subMenuItem.setSubMenu(() -> {
                GMenuList thirdSubMenu = new GMenuList();
                thirdSubMenu.setWidth(100.0F);

                GMenuItem subSubMenuItem = new GMenuItem(thirdSubMenu.getWidth(), thirdSubMenu.getMenuItemHeight(), "三级菜单", FontHelper.getFont(FontKeys.SIM_HEI_14));
                thirdSubMenu.addElement(subSubMenuItem);
                subSubMenuItem.setSplitLine(false);

                return thirdSubMenu;
            });

            subMenu.addElement(subMenuItem);
            return subMenu;
        });

        GMenuItem menuItem3 = new GMenuItem(menuList.getWidth(), menuList.getMenuItemHeight(), "item3", FontHelper.getFont(FontKeys.SIM_HEI_14));
        menuItem3.setSplitLine(false);

        menuList.addElement(menuItem1);
        menuList.addElement(menuItem2);
        menuList.addElement(menuItem3);

        formManager.addContainer(menuList);

        GMenuBar menuBar = new GMenuBar();
        menuBar.setBackground(Color.SLATE);
        menuBar.setHeight(22.0F);

        GMenuBar.GMenuBarItem fileMenu = new GMenuBar.GMenuBarItem(48, menuBar.getHeight(), "文件", FontHelper.getFont(FontKeys.SIM_HEI_14)) {
            @Override
            public boolean isHotKeyPressed() {
                return Gdx.input.isKeyPressed(Input.Keys.F);
            }
        };

        fileMenu.setSubMenu(() -> {
            GMenuList fileMenuList = new GMenuList();
            fileMenuList.setWidth(200.0F);

            GMenuItem newFile = new GMenuItem(100.0F, fileMenuList.getMenuItemHeight(), "新建", FontHelper.getFont(FontKeys.SIM_HEI_14));
            GMenuItem open = new GMenuItem(100.0F, fileMenuList.getMenuItemHeight(), "打开", FontHelper.getFont(FontKeys.SIM_HEI_14)) {
                @Override
                public void onClick() {
                    GFileSelectWindow fileSelectWindow = new GFileSelectWindow(FontHelper.getFont(FontKeys.SIM_HEI_14));
                    fileSelectWindow.setTitle("打开");
                    fileSelectWindow.setPath("C:\\");
                    fileSelectWindow.setConfirmCallback((path, selectedFiles) -> {
                        System.out.println("当前路径：" + path);
                        for (File file : selectedFiles) {
                            System.out.println("选择的文件：" + file.getAbsolutePath());
                        }
                    });
                }
            };
            open.setSplitLine(false);

            fileMenuList.addElement(newFile);
            fileMenuList.addElement(open);

            return fileMenuList;
        });
        GMenuBar.GMenuBarItem editMenu = new GMenuBar.GMenuBarItem(48, menuBar.getHeight(), "编辑", FontHelper.getFont(FontKeys.SIM_HEI_14));
        GMenuBar.GMenuBarItem viewMenu = new GMenuBar.GMenuBarItem(48, menuBar.getHeight(), "视图", FontHelper.getFont(FontKeys.SIM_HEI_14));

        menuBar.addElement(fileMenu);
        menuBar.addElement(editMenu);
        menuBar.addElement(viewMenu);

        formManager.addContainer(menuBar);
    }

    @Override
    public void resize(int x, int y) {
    }

    @Override
    public void render() {
        Gdx.gl20.glClearColor(0.16863F, 0.16863F, 0.16863F, 1);
        Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
        formManager.update();
        sb.begin();
        formManager.render(sb);
        sb.end();
    }

    @Override
    public void pause() {
        formManager.pause();
    }

    @Override
    public void resume() {
        formManager.resume();
    }

    @Override
    public void dispose() {
        formManager.dispose();
    }
}
